class Admin::BlogPostsController < AdminController

  def index
    @blog_posts = BlogPost.paginate(:page => params[:page], :order => 'created_at DESC')
    @tags       = Tag.all
    @page_title = "Blog Posts"
  end
  
  def new
    @blog_post  = BlogPost.new
    @tags       = Tag.all
    @page_title = "Create a Blog Post"
  end

  def create
    @blog_post = BlogPost.new(params[:blog_post])
    @blog_post.author_id = current_user.id
    
    if params[:commit] == "Save & Publish Changes"
      @blog_post.is_published = true
      @blog_post.published_at = Time.now.to_s(:db)
    end
    
    if @blog_post.save
      flash[:notice] = "Successfully created admin/blog posts."
      redirect_to admin_blog_posts_url
    else
      render :action => 'new'
    end
  end

  def edit
    @blog_post  = BlogPost.find(params[:id])
    @tags       = Tag.all
    @page_title = "Updating #{@blog_post.title}"
  end

  def update
    @blog_post = BlogPost.find(params[:id])
    
    @blog_post.author_id = current_user.id
    
    if params[:commit] == "Save & Publish Changes"
      @blog_post.is_published = true
      @blog_post.published_at = Time.now.to_s(:db)
    end
    
    if @blog_post.update_attributes(params[:blog_post])
      flash[:notice] = "Successfully updated admin/blog posts."
      redirect_to admin_blog_posts_url
    else
      render :action => 'edit'
    end
  end

  def destroy
    @blog_post = BlogPost.find(params[:id])
    @blog_post.destroy
    flash[:notice] = "Successfully destroyed admin/blog posts."
    redirect_to root_url
  end
end
